extends Node

const INFO_DIALOG = preload("res://ui/info_dialog/info_dialog.tscn")
var text_arr_2_show: Array
var current_text_index:int = 0
var is_showing_text: bool = false
var current_text_dialog_ui: InfoDialog = null


func show_text_arr(text_arr: Array):
	if text_arr.size() <= 0:
		push_error("show_info text arr size <= 0")
		return
		
	if is_showing_text: return
		
	GameManager.player_input_locked = true
	
	text_arr_2_show = text_arr
	current_text_index = 0
	
	show_text(text_arr_2_show[current_text_index])
	is_showing_text = true
	current_text_index += 1
	

func show_text(text):
	if not current_text_dialog_ui:
		current_text_dialog_ui = INFO_DIALOG.instantiate()
		get_tree().root.add_child(current_text_dialog_ui)
		
	current_text_dialog_ui.diaplay_text(text)
	

func _physics_process(delta: float) -> void:
	process_text_display()
	
	
func process_text_display():
	if not is_showing_text: return
	
	if Input.is_action_just_pressed("interact") and current_text_dialog_ui.is_finish():
		if current_text_index >= text_arr_2_show.size():
			call_deferred("finish_show_text_arr")
		else:
			show_text(text_arr_2_show[current_text_index])
			current_text_index += 1
	elif Input.is_action_just_pressed("interact") and not current_text_dialog_ui.is_finish():
		current_text_dialog_ui.show_complete_text()
			

func finish_show_text_arr():
	if current_text_dialog_ui: 
		current_text_dialog_ui.queue_free()
		current_text_dialog_ui = null
		
	is_showing_text = false
	GameManager.player_input_locked = false
			
	
	
